package jucTest;


import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class SynchronousBlockingQueueDemo {
    public static void main(String[] args)

    {
        SynchronousQueue<String> queue = new SynchronousQueue<>();
        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName()+" put 1");
                queue.put("1");
                System.out.println(Thread.currentThread().getName()+" put 2");
                queue.put("2");
                System.out.println(Thread.currentThread().getName()+" put 3");
                queue.put("3");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
                queue.take();
                System.out.println(Thread.currentThread().getName()+" take 1");
                queue.take();
                System.out.println(Thread.currentThread().getName()+" take 2");
                queue.take();
                System.out.println(Thread.currentThread().getName()+" take 3");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }
}
